package com.winit.deploy.manager;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.util.DateUtil;
import com.winit.common.util.JSONFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by wangjian on 17/3/17.
 */
@Component
public class DeployAuthorizeManager {
    private static final Logger logger = LoggerFactory.getLogger(DeployAuthorizeManager.class);
    private static final String authorize_file = "/usr/local/Beetle/release/authorize";
    private static final String stop_deploy_file = "/usr/local/Beetle/release/stopdeploy";

    //查询授权
    public JSONObject getAuthorize() {
        JSONObject object = new JSONObject();
        File file = new File(authorize_file);
        if (!file.exists()) {
            object.put("authorize", false);
            logger.info("com.winit.deploy.manager.DeployAuthorizeManager.getAuthorize 授权文件不存在");
            return object;
        }
        String time = JSONFileUtil.getFileString(authorize_file);
        if (StringUtils.isEmpty(time)) {
            object.put("authorize", false);
            logger.info("com.winit.deploy.manager.DeployAuthorizeManager.getAuthorize 授权文件为空");
            return object;
        }

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = new Date();
            Date authorize_time = sdf.parse(time);
            if (now.after(authorize_time)) {
                object.put("authorize", false);
            } else {
                object.put("authorize", true);
                object.put("authorize_time", time);
            }
        } catch (ParseException e) {
            object.put("authorize", false);
            logger.error("com.winit.deploy.manager.DeployAuthorizeManager.getAuthorize 异常", e);
        }
        return object;
    }

    //授权
    public void doAuthorize(String hours) throws Exception {
        try {
            String time = DateUtil.dateAddHour(new Date(), Integer.parseInt(hours), "yyyy-MM-dd HH:mm:ss");
            JSONFileUtil.saveFile(authorize_file, time);
        } catch (Exception e) {
            logger.error("com.winit.deploy.manager.DeployAuthorizeManager.doAuthorize 异常", e);
            throw new Exception("授权发布线上环境失败." + e.getLocalizedMessage());
        }
    }

    //取消授权
    public void cancelAuthorize() throws Exception {
        try {
            JSONFileUtil.saveFile(authorize_file, "");
        } catch (Exception e) {
            logger.error("com.winit.deploy.manager.DeployAuthorizeManager.cancelAuthorize 异常", e);
            throw new Exception("取消授权失败." + e.getLocalizedMessage());
        }
    }

    //查询是否终止环境发布
    public boolean getStopDeploytip(String env) {
        File file = new File(stop_deploy_file);
        if (!file.exists()) {
            return false;
        }
        JSONObject object = JSONFileUtil.getFileJsonForBean(stop_deploy_file, JSONObject.class);
        logger.info("com.winit.deploy.manager.DeployAuthorizeManager.getStopDeploytip env=" + env + ",flag=" + object.getBoolean(env));
        return object.getBoolean(env);
    }

    public void updateStopDeployTip(String env, boolean flag) {
        logger.info("com.winit.deploy.manager.DeployAuthorizeManager.updateStopDeployTip env=" + env + ",flag=" + flag);
        File file = new File(stop_deploy_file);
        JSONObject object = new JSONObject();
        if (file.exists()) {
            object = JSONFileUtil.getFileJsonForBean(stop_deploy_file, JSONObject.class);
        }
        object.put(env, flag);
        JSONFileUtil.saveFileJson(stop_deploy_file, object);
    }
}
